﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.IO.Ports;
using System.IO;
using Newtonsoft.Json;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Drawing.Design;
using System.Xml;

namespace FrameDetectionTest
{
    public class Settings
    {
        [Category("CIM"), DescriptionAttribute("是否启用CIM,该选项决定了是否自动测试")]
        public bool IsEnableCIM { get; set; }
        [Category("重测次数"), DescriptionAttribute("测试失败后重复测试的次数")]
        public int RetestTime { get; set; } = 10;
        [Category("CIM"), DescriptionAttribute("CIM系统参数设定"), TypeConverter(typeof(CIMIDList))]
        public string EquipmentId { get; set; }
        [CategoryAttribute("Project"), DescriptionAttribute("选择测试工程文件路径"), EditorAttribute(typeof(FolderBrowser), typeof(UITypeEditor))]
        public string ProjectPath { get; set; } = AppDomain.CurrentDomain.BaseDirectory + "Project";
        [CategoryAttribute("Project"), DescriptionAttribute("选择测试工程目录文件路径"), EditorAttribute(typeof(FolderBrowser), typeof(UITypeEditor))]
        public string ListPath { get; set; } = AppDomain.CurrentDomain.BaseDirectory + "Project";
        [Category("Camera"), DescriptionAttribute("是否从相机获取图片")]
        public bool IsCameraEnable { get; set; } = true;
        [Category("Camera"), DescriptionAttribute("线程等待获取流数据的间隔时间(毫秒)，勿动")]
        public int GetFrameDataIntervalTime { get; set; } = 600;
        [CategoryAttribute("Log图片保存路径"), DescriptionAttribute("请建立公盘映射后再开启此功能\n关闭则保存本地，打开则保存公盘")]
        public bool LogoSaveSwitch { get; set; } = false;
        [CategoryAttribute("Log图片保存路径"), DescriptionAttribute("选择测试工程文件公盘路径(测试中)"), EditorAttribute(typeof(FolderBrowser), typeof(UITypeEditor))]
        public string LogPublicSaveAddress { get; set; } = "Z:\\FrameDetectionLog";
        [CategoryAttribute("亮线检测开关"), DescriptionAttribute("用于检测细微亮线，但可能会造成误判，但不会打下不良，只会报警")]
        public bool BrightLineEnable { get; set; } = false;
        [CategoryAttribute("Template"), DescriptionAttribute("选择测试模板文件所有目录"), EditorAttribute(typeof(FolderBrowser), typeof(UITypeEditor))]
        public string TemplatePath { get; set; }
        [Category("Match"), DescriptionAttribute("图像匹配设定: 得分设定")]
        public int MatchScore { get; set; } = 50;
        [Category("Match"), DescriptionAttribute("图像匹配设定：阀值设定")]
        public double UniquenessThreshold { get; set; } = 0.65;
        [Category("Match"), DescriptionAttribute("图像匹配设定：匹配超时")]
        public double MatchTimeOut { get; set; } = 45000;
        [Category("Match"), DescriptionAttribute("图像匹配设定：匹配成功次数")]
        public double MatchTSuccessCountLimit { get; set; } = 1;
    }
    class FolderBrowser : UITypeEditor
    {
        private FolderBrowserDialog dlg = new FolderBrowserDialog();
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (dlg.ShowDialog().Equals(DialogResult.OK))
            {
                return dlg.SelectedPath;
            }

            return value;
        }
    }// FolderBrowser
    class CIMIDList : StringConverter
    {
        //true enable,false disable
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(GetCIMIDs(eIDType.CIM_PWR)); //编辑下拉框中的items

        }
        private static string[] GetCIMIDs(eIDType Type)
        {
            string IDsString = string.Empty;

            try
            {
                XmlDocument XmlDoc = new XmlDocument();
                XmlDoc.Load("IDs.xml");
                XmlNode xn = XmlDoc.SelectSingleNode("IDs");
                foreach (XmlNode v in xn)
                {
                    if (v.Name == "CIM")
                    {
                        XmlNodeList cxn = v.ChildNodes;
                        foreach (XmlNode cv in cxn)
                        {
                            string ItemName = GetIDsItemName(Type);
                            if (cv.Name == ItemName)
                            {
                                IDsString = cv.InnerText;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            List<string> Items = new List<string>();
            foreach (string item in IDsString.Split(','))
            {
                Items.Add(item.Trim().ToUpper());
            }

            return Items.ToArray();
        }

        // true: disable text editting.    false: enable text editting;
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
        private static string GetIDsItemName(eIDType Type)
        {
            string ItemName = string.Empty;
            switch (Type)
            {
                case eIDType.CIM_EDID:
                    ItemName = "EDID";
                    break;
                case eIDType.CIM_HPT_GBT:
                    ItemName = "HPT_GBT";
                    break;
                case eIDType.CIM_WB:
                    ItemName = "WB";
                    break;
                case eIDType.CIM_PWR:
                    ItemName = "PWR";
                    break;
                case eIDType.MES_EDID:
                    ItemName = "EDID";
                    break;
                case eIDType.MES_HPT_GBT:
                    ItemName = "HPT_GBT";
                    break;
                case eIDType.MES_WB:
                    ItemName = "WB";
                    break;
                case eIDType.MES_PWR:
                    ItemName = "PWR";
                    break;
                case eIDType.MES_ER:
                    ItemName = "ER";
                    break;
                case eIDType.Other:
                    ItemName = "Other";
                    break;
            }
            return ItemName;
        }
        private enum eIDType
        {
            CIM_EDID,
            CIM_HPT_GBT,
            CIM_WB,
            CIM_PWR,
            MES_EDID,
            MES_HPT_GBT,
            MES_WB,
            MES_PWR,
            MES_ER,
            Other,
        }
    }//  CIMIDList
}
